import { Route, Outlet } from 'react-router-dom';
import NotFound from 'component/NotFound';

export function createRoute(routes) {
  const routerElements = [];

  for (let route of routes) {
    let cur;
    if (route.children) {
      const children = createRoute(route.children);
      let LayoutComponent = route.layout?.component || Outlet;
      cur = (
        <Route key={route.key || route.path} path={route.path} element={<LayoutComponent />}>
          {children}
        </Route>
      );
    } else {
      let Element = route.component;
      cur = <Route key={route.key || route.path} path={route.path} element={<Element />} />;
    }
    routerElements.push(cur);
  }
  routerElements.push(<Route key="not-found" path="*" element={<NotFound />} />);
  return routerElements;
}
